from django.http import HttpResponseRedirect
from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse
from datetime import date

from forms import *
from timesheet.forms import *

from timesheet.models import *
from financier.models import *

from functions import *


def financier(request, template_financier='financier_timesheet.html'):
    """
        View for page of the financier
        
    """
    try:
        Financier.objects.get(financier=request.user)
        id_user = request.user.id
        first_name = request.user.first_name
        last_name = request.user.last_name
        users_form = UsersForm()
        projects = Project.objects.all()
        projects_form = ProjectsForm(projects)
        date_form = DateIntervalForm()
        choice_form = FinancierChoiceForm()
        css_media = FinancierMediaForm()
        summ1 = ''
        summ2 = ''
        summ3 = ''
        if (request.method == 'GET'):
            get = request.GET.copy()
            if ((('initial_date' and 'end_date') in get) and get['initial_date']
                and get['end_date']):
                if (get['initial_date'] > get['end_date']):
                    return HttpResponseRedirect(request.path)
                if ('UserHours' in get):
                    choice_form = FinancierChoiceForm(initial={'choices': 1})
                    users_form = UsersForm({'users': get['users']})
                    date_form = DateIntervalForm({
                                 'initial_date': get['initial_date'],
                                 'end_date': get['end_date']})
                    if ((users_form.is_valid()) and (date_form.is_valid())):
                        times = Time.objects.take_time_interval_date(
                                get['users'], date_form.cleaned_data['initial_date'],
                                date_form.cleaned_data['end_date'])
                        try:
                            summ1 = summation_time(times)
                        except(Warning):
                            return HttpResponseRedirect(request.path)
                if ('UserProjectHours' in get):
                    choice_form = FinancierChoiceForm(initial={'choices': 2})
                    users_form = UsersForm({'users': get['users']})
                    projects_form = ProjectsForm(projects, {'projects': get['projects']})
                    date_form = DateIntervalForm({
                                 'initial_date': get['initial_date'],
                                 'end_date': get['end_date']})
                    if ((users_form.is_valid()) and (date_form.is_valid())
                         and (projects_form.is_valid())):
                        times = Time.objects.take_project_time_interval_date(
                                                get['users'], get['projects'],
                                      date_form.cleaned_data['initial_date'],
                                          date_form.cleaned_data['end_date'],)
                        try:
                            summ2 = summation_time(times)
                        except(Warning):
                            return HttpResponseRedirect(request.path)
                if ('ProjectHours' in get):
                    choice_form = FinancierChoiceForm(initial={'choices': 3})
                    projects_form = ProjectsForm(projects, {'projects': get['projects']})
                    date_form = DateIntervalForm({
                                 'initial_date': get['initial_date'],
                                 'end_date': get['end_date']})
                    if ((date_form.is_valid()) and (projects_form.is_valid())):
                        times = Time.objects.take_project_all_time_interval_date(get['projects'],
                                                         date_form.cleaned_data['initial_date'],
                                                             date_form.cleaned_data['end_date'],)
                        try:
                            summ3 = summation_time(times)
                        except(Warning):
                            return HttpResponseRedirect(request.path)
        return render_to_response(template_financier, {'date_form': date_form,
                                                       'projects_form': projects_form, 
                                                       'first_name': first_name,
                                                       'last_name': last_name,
                                                       'css_media': css_media,
                                                       'users_form': users_form,
                                                       'summ1': summ1,
                                                       'summ2': summ2,
                                                       'summ3': summ3,
                                                       'choice_form': choice_form})
    except(Financier.DoesNotExist):
        return HttpResponseRedirect(reverse('invalid'))
